package com.specialtreasure.freightdepot.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.core.util.DateUtils;
import com.specialtreasure.freightdepot.R;
import com.specialtreasure.freightdepot.model.CUserData;
import com.specialtreasure.freightdepot.model.TbItemOrder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/23 0023.
 * 订单适配器
 */

public class MyOrderAdapter extends BaseAdapter {

    List<TbItemOrder> tbItemOrders = new ArrayList<TbItemOrder>();
    Context context;

    public MyOrderAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        if (tbItemOrders != null && tbItemOrders.size() > 0) {
            return tbItemOrders.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return tbItemOrders.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder viewHolder = null;
        if (null == view) {
            view = View.inflate(viewGroup.getContext(), R.layout.item_order_list, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        //设置信息
        final TbItemOrder tbItemOrder = (TbItemOrder) getItem(i);
        if (tbItemOrder != null) {
            viewHolder.tv_car_date.setText(DateUtils.getCurremtMonth(tbItemOrder.getCreateTime()));
            viewHolder.tv_car_address.setText(tbItemOrder.getOrderContent());
            viewHolder.tv_transCharge.setText("运费："+tbItemOrder.getTransCharge());
            viewHolder.tv_messagefee.setText("信息费："+tbItemOrder.getItemCharge());

            final CUserData cUserData = tbItemOrder.getcUserData();
            if (cUserData != null) {
                viewHolder.tv_name.setText(cUserData.getCertName());
                viewHolder.tv_car_number.setText(cUserData.getCarNumber());
                viewHolder.tv_car_phone.setText(cUserData.getPhone());

                //打电话
                viewHolder.iv_telphone.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_DIAL);
                        intent.setData(Uri.parse("tel:" + cUserData.getPhone()));
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);
                    }
                });
            }

        }


        return view;
    }


    public void setTbItemOrders(List<TbItemOrder> tbItemOrders) {
        this.tbItemOrders = tbItemOrders;
    }

    public static class ViewHolder {
        public View rootView;
        public TextView tv_name;
        public TextView tv_car_number;
        public TextView tv_car_phone;
        public TextView tv_car_date;
        public TextView tv_car_address;
        public TextView tv_transCharge;
        public TextView tv_messagefee;
        public ImageView iv_telphone;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.tv_name = (TextView) rootView.findViewById(R.id.tv_name);
            this.tv_car_number = (TextView) rootView.findViewById(R.id.tv_car_number);
            this.tv_car_phone = (TextView) rootView.findViewById(R.id.tv_car_phone);
            this.tv_car_date = (TextView) rootView.findViewById(R.id.tv_car_date);
            this.tv_car_address = (TextView) rootView.findViewById(R.id.tv_car_address);
            this.tv_transCharge = (TextView) rootView.findViewById(R.id.tv_transCharge);
            this.tv_messagefee = (TextView) rootView.findViewById(R.id.tv_messagefee);
            this.iv_telphone = (ImageView) rootView.findViewById(R.id.iv_telphone);
        }

    }
}
